package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.ProfesIndicatorTwo;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.ProfesIndicatorTwoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理毕业生能力要求二级指标信息")
@CrossOrigin
@RestController
@RequestMapping("/ProfesIndicatorTwos")
public class ProfesIndicatorTwoConreoller {

    @Autowired
    private ProfesIndicatorTwoService profesIndicatorTwoService;

    @ApiOperation(value = "分页查询二级指标列表")
    @GetMapping("/AllProfesIndicatorTwo")
    public String getAllIndicatorTwoList(QueryInfo queryInfo) {
        int numbers = profesIndicatorTwoService.getIndicatorTwoCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<ProfesIndicatorTwo> profesIndicatorTwos = profesIndicatorTwoService.getAllIndicatorTwoList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data", profesIndicatorTwos);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加二级指标")
    @PostMapping("/ProfesIndicatorTwo")
    public String addIndicatorTwo(@RequestBody ProfesIndicatorTwo profesIndicatorTwo) {
        int result;
        try{
            result = profesIndicatorTwoService.addIndicatorTwo(profesIndicatorTwo);
        }
        catch(Exception e) {
            result = 0;
        }
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "删除指定二级指标")
    @DeleteMapping("/ProfesIndicatorTwo")
    public String deleteIndicatorTwo(String sindicname) {
        int result = profesIndicatorTwoService.deleteIndicatorTwo(sindicname);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "查询指定二级指标信息")
    @GetMapping("/ProfesIndicatorTwoMessage")
    public String getUpdateIndicatorTwo(String sindicname) {
        ProfesIndicatorTwo updateProfesIndicatorTwo = profesIndicatorTwoService.getUpdateIndicatorTwo(sindicname);
        String users_json = JSON.toJSONString(updateProfesIndicatorTwo);
        return users_json;
    }

    @ApiOperation(value = "更新指定二级指标信息")
    @PutMapping("/ProfesIndicatorTwo")
    public String updateIndicatorTwo(@RequestBody ProfesIndicatorTwo profesIndicatorTwo) {
        int result = profesIndicatorTwoService.updateIndicatorTwo(profesIndicatorTwo);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }
}
